<?php
namespace Admin\Controller;
use Think\Controller;

class IndexController extends Controller {
	public function index(){
		if(empty($_SESSION['user'])){
			$this->redirect('login');
		}
		$this->display();
	}

	public function login(){
		
		$this->display();
	}
	
	public function dologin(){
		$name=I('name');
		$password=md5(I('password'));
		$user=M('admin')->where("name='{$name}' && pwd='{$password}'")->find();
		if(empty($user)){
			$_SESSION['loginerror']=1;
			$this->redirect('login');
		}
		unset($_SESSION['loginerror']);
		$_SESSION['user']=$user;
		$this->redirect('index');
	}

	public function loginout(){
		unset($_SESSION['user']);
		$this->redirect('login');
	}
	//加载添加图片模板
	public function pictureadd(){
		$this->display();
	}

	public function picturelist(){
		$User = M('list'); // 实例化User对象
		$count      = $User->count();// 查询满足要求的总记录数
		$Page       = new \Think\Page($count,20);// 实例化分页类
		//传入总记录数和每页显示的记录数(25)
		$show       = $Page->show();// 分页显示输出
		// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
		$list = $User->order('id asc')->limit($Page->firstRow.','.$Page->listRows)->select();
		// 赋值数据集
		$this->assign('info',$list);
		//赋值分页输出
		$this->assign('page',$show); 
		// 输出模板
		// dump($show);
		$this->display();
	}

	public function picturedel(){
		$id=I('id');
		$info=M('user')->where("user_id={$id}")->find();
		$head=$info['head'];
		$code=$info['code'];
		$path='./Public/Uploads/';
		$codepath='./Public/IMG/';
		$headimg=$path.$head;
		$codeimg=$codepath.$code;
		unlink($headimg);
		unlink($codeimg);
		$info=M('user')->where("user_id={$id}")->delete();
	}

	public function picture_edit(){
		$id=I('id');
		$data=M('user')->where("user_id={$id}")->select();
		$this -> assign("data",$data);
		$this -> display();
	}



	public function ajax(){
		$data['time']  = I('time');
		$data['name']  = I('name');
		$data['tel']  = I('tel');
		$data['otherway']  = I('otherway');
		$data['mes']  = I('mes');
		$res=M('list')->add($data);
		if($res > 0){
			$this->ajaxReturn(array('status'=>1));
		}
	}



}